package com.itheima.dao;

import com.itheima.bean.Student;
import com.itheima.utils.JDBCUtils;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

@SuppressWarnings("all")
public class StudentDaoImpl implements StudentDaoInter {
    private ArrayList<Student> list = new ArrayList<>();
    Student stu = null;
    Connection con = JDBCUtils.getConnection();
    PreparedStatement ps = null;
    ResultSet res = null;

    @Override
    public ArrayList<Student> findAll() {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db10", "root", "root");
            String sql = "select * from student";
            ps = this.con.prepareStatement(sql);
            res = ps.executeQuery();
            while (res.next()) {
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                stu = new Student(sid, name, age, birthday);
                list.add(stu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res,ps,con);
        }
        return list;
    }

    @Override
    public Student findById(Integer id) {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db10", "root", "root");
            String sql = "select * from student where sid = ?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, id);
            res = ps.executeQuery();
            if (res.next()) {
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                stu = new Student(sid, name, age, birthday);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res,ps,con);
        }
        return stu;
    }

    @Override
    public int insert(Student stu) {
        int res = 0;
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db10", "root", "root");
            String sql = "insert into student value (?,?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setObject(1, stu.getSid());
            ps.setObject(2, stu.getName());
            ps.setObject(3, stu.getAge());
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(stu.getBirthday());
            ps.setObject(4, birthday);
            res = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,ps,con);
        }
        return res;
    }

    @Override
    public int update(Student stu, Integer id) {
        int res = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db10", "root", "root");
            String sql = "update student set sid = ?,name = ?,age = ?,birthday = ? where sid = ?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, id);
            ps.setObject(2, stu.getName());
            ps.setObject(3, stu.getAge());
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(stu.getBirthday());
            ps.setObject(4, birthday);
            ps.setObject(5, stu.getSid());
            res = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,ps,con);
        }
        return res;
    }

    @Override
    public int delete(Integer id) {
        int res = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db10", "root", "root");
            String sql = "delete from student where sid = ?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, id);
            res = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,ps,con);
        }
        return res;
    }
}
